package com.haohuo.sort;

import java.util.Arrays;

/**
 * 插入排序
 * note：
 * <p>
 * author: Caozhitong
 **/
public class InsertSort {

    public static void main(String[] args) {

        int[] elements = new int[]{1211, 212, 31, 422, 5222, 216, 712, 72, 733, 232, 453, 243, 233, 232};
        sort(elements);
        Arrays.stream(elements).forEach(element -> System.out.print(element + ","));
    }

    public static void sort(int[] elements) {
        for (int i = 1; i < elements.length; i++) {
            for (int j = i; j > 0; j--) {
                if (elements[j] > elements[j - 1]) {
                    int temp = elements[j];
                    elements[j] = elements[j - 1];
                    elements[j - 1] = temp;
                } else {
                    break;
                }
            }
        }
    }

    /**
     * 数组元素交换
     *
     * @param elements
     * @param i
     * @param j
     */
    private static void swap(int[] elements, int i, int j) {
        if (i != j) {
            int temp = elements[i];
            elements[i] = elements[j];
            elements[j] = temp;
        }
    }
}
